package com.kung.qvs.ui.main.fragment.manage;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.Navigation;

import com.kung.qvs.BR;
import com.kung.qvs.R;
import com.kung.qvs.app.AppViewModelFactory;
import com.kung.qvs.databinding.FragWorkerEditBinding;
import com.kung.qvs.entity.WorkerBean;
import com.kung.qvs.ui.widget.ConformDialog;

import me.goldze.mvvmhabit.base.BaseFragment;

public class FragWorkerAdd extends BaseFragment<FragWorkerEditBinding, FragWorkerAddModel> {

    private WorkerBean workerBean;
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.frag_worker_edit;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public FragWorkerAddModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(getActivity(), factory).get(FragWorkerAddModel.class);
    }

    @Override
    public void initParam() {
        super.initParam();
        if (getArguments() != null) {
            workerBean = (WorkerBean) getArguments().getSerializable("worker");
        }
    }

    @Override
    public void initData() {
        super.initData();
        if (workerBean != null) {
            viewModel.mIsUpdate.set(true);
            viewModel.mWorkerId.set(workerBean.getUserId());
            viewModel.mWorkerName.set(workerBean.getNickName());
            viewModel.mWorkerAccount.set(workerBean.getUserName());
        } else {
            viewModel.mIsUpdate.set(false);
            viewModel.mWorkerId.set("");
            viewModel.mWorkerName.set("");
            viewModel.mWorkerAccount.set("");
        }
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.eventToLastPage.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                Navigation.findNavController(getView()).navigateUp();
            }
        });

        viewModel.uc.eventSaveWorker.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                ConformDialog.Builder builder = new ConformDialog.Builder(getActivity());
                builder.setMessage("是否保存工人？", "");
                builder.setTitle("保存工人");
                builder.setPositiveButton("确认",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog2,
                                                int which) {
                                dialog2.dismiss();
                                viewModel.funSaveWorker();
                            }
                        });

                builder.setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog2,
                                                int which) {
                                dialog2.dismiss();
                            }
                        });
                builder.create().show();
            }
        });
    }
}
